package com.excel2sql.chenjun.ui;

import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Lists;

public class ControlWindow extends MyJframe
{
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -2530184913269688738L;
	
	private Panel panelContainer;
	
	private GridLayout gridLayout = new GridLayout(3, 1);
	
	private JLabel jLabel;
	
	private JButton button_insert;
	
	private JButton button_update;
	
	private List<String> headList;
	
	private List<List<String>> datas = Lists.newArrayList();
	
	public void initWindow()
	{
		super.setTitle("sql生成工具");
		super.setBounds(480, 200, 400, 300);
		// new出各种可视化控件
		panelContainer = new Panel(gridLayout);
		jLabel = new JLabel("选择其中的一个操作，劳资来帮你实现", JLabel.CENTER);
		button_insert = new JButton("生成insert语句");
		button_update = new JButton("生成update语句");
		this.add(panelContainer);
		panelContainer.add(jLabel);
		panelContainer.add(button_insert);
		panelContainer.add(button_update);
		this.setVisible(true);
		
		this.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				super.windowClosing(e);
				System.exit(0);
			}
		});
		
		//TODO
		button_insert.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				StringBuilder resultSQL = new StringBuilder("");
				StringBuilder columnStr = new StringBuilder("(");
				for(String s : headList)
				{
					columnStr.append(s);
					//如果不是最后一列
					if( ! s.equals(headList.get(headList.size()-1)))
					{
						columnStr.append(",");
					}
				}
				columnStr.append(")");
				for(List<String> row : datas)
				{
					StringBuilder columnValues = new StringBuilder("(");
					for(int i=0;i<row.size();i++)
					{
						String s = row.get(i);
						if(s == null)
						{
							s = "";
						}
						columnValues.append("'");
						columnValues.append(s);
						columnValues.append("'");
						//如果不是最后一个列值
						if( i != row.size()-1)
						{
							columnValues.append(",");
						}
					}
					resultSQL.append("insert into [TABLE_NAME]")
						.append(columnStr.toString())
						.append("values")
						.append(columnValues.toString())
						.append(");\n");
				}
				System.out.println("最终生成结果="+resultSQL);
				hideWindow();
				ResultWindow resultWindow = new ResultWindow();
				resultWindow.setResult(resultSQL.toString());
				resultWindow.showWindow();
				
			}
		});
		
		//TODO
		button_update.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				StringBuilder resultSql = new StringBuilder("");
				StringBuilder columnSet = new StringBuilder("");
				for(List<String> row : datas)
				{
					StringBuilder sql = new StringBuilder("update [TABLE_NAME] ");
				}
				hideWindow();
				ResultWindow resultWindow = new ResultWindow();
				resultWindow.showWindow();
			}
		});
	}
	
	public void showWindow()
	{
		initWindow();
		System.err.println("datas = "+datas);
		System.err.println("headList = "+headList);
	}
	
	private void hideWindow()
	{
		this.setVisible(false);
	}
	
	public static void main(String[] args)
	{
		ControlWindow controlWindow = new ControlWindow();
		controlWindow.showWindow();
	}

	public List<String> getHeadList()
	{
		return headList;
	}

	public void setHeadList(List<String> headList)
	{
		this.headList = headList;
	}

	public List<List<String>> getDatas()
	{
		return datas;
	}

	public void setDatas(List<List<String>> datas)
	{
		this.datas = datas;
	}
	
}
